package com.bootdo.app.controller;


import com.bootdo.app.service.AppSellerService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.*;
import com.bootdo.appsystem.service.EvaluateService;
import com.bootdo.appsystem.service.ImageinfoService;
import com.bootdo.appsystem.service.OrderproductitemsService;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.utils.HttpContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 评论相关
 */
@Controller
@RequestMapping("/app/evaluate/")
public class AppEvaluateController {

    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private AppSellerService appSellerService;
    @Autowired
    private OrderproductitemsService orderproductitemsService;
    @Autowired
    private ImageinfoService imageinfoService;

    /**
     * 去评价页面
     * @param oitemid
     * @param ordercode
     * @param model
     * @return
     */
    @GetMapping("toAddGoodsEvaluate")
    public String toAddGoodsEvaluate(Integer oitemid ,String ordercode, Model model){
        OrderproductitemsDO orderproductitemsDO = orderproductitemsService.get(oitemid);
        UseraccountDO useraccountDO = (UseraccountDO) HttpContextUtils.getSessionObject(AppUtilXML.SESSION_CUSTOMER);

        HashMap<String, Object> map = new HashMap<>();
        map.put("uid" , useraccountDO.getUid());
        map.put("pid" , orderproductitemsDO.getPid());
        map.put("ordercode" , ordercode);
        List<EvaluateDO> list = evaluateService.list(map);
        EvaluateDO evaluateDO = new EvaluateDO();
        if(list.size() == 0){
            evaluateDO.setIsshow(0);
            evaluateDO.setIsimage(0);
            evaluateDO.setPid(orderproductitemsDO.getPid());
            evaluateDO.setSellerid(orderproductitemsDO.getSellerid());
            evaluateDO.setUid(useraccountDO.getUid());
            evaluateDO.setOrdercode(ordercode);
            evaluateService.save(evaluateDO);
        }else {
            evaluateDO = list.get(0);
        }
        model.addAttribute("oitemid",oitemid);
        model.addAttribute("evaluateDO" , evaluateDO);
        return "app/my/order/addevaluate";
    }


    /**
     * 上传数据
     * @param evaluateDO
     * @return
     */
    @ResponseBody
    @PostMapping("applyevaluate")
    public ServerResponse applyevaluate(EvaluateDO evaluateDO , Integer oitemid){
        OrderproductitemsDO orderproductitemsDO = new OrderproductitemsDO();
        orderproductitemsDO.setOitemid(oitemid);
        orderproductitemsDO.setGoodsstatus(4);
        orderproductitemsService.update(orderproductitemsDO);


        EvaluateDO evaluate = evaluateService.get(evaluateDO.getEid());
        evaluate.setStar(evaluateDO.getStar());
        evaluate.setDetails(evaluateDO.getDetails());
        evaluate.setAddtime(new Date());
        evaluate.setTitle("已上传评价");
        int update = evaluateService.update(evaluate);
        if(update > 0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    /**
     *【方法解释】上传商家图片接口
     *【参数】
     */
    @ResponseBody
    @PostMapping("msgapplysellerimages")
    public ServerResponse msgAddSellerImages(@RequestParam("file") MultipartFile file , HttpServletRequest request , Integer eid){
        if(file == null){
            return ServerResponse.createByErrorMessage("未上传图片");
        }

        //商家上传图片
        UseraccountDO useraccountDO = (UseraccountDO)request.getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        FileDO sysFile = appSellerService.uploadEvaluateImage(file,useraccountDO);

//      将上传文件的信息存放在数据库中。数据库为ImageInfo
        ImageinfoDO imageinfoDO = new ImageinfoDO();
        imageinfoDO.setType(22556);
        imageinfoDO.setAllid(eid);
        imageinfoDO.setDisplay(0);
        imageinfoDO.setImgurl(sysFile.getUrl());
        imageinfoDO.setImgdesc("评价图片");
        imageinfoDO.setImagetype(0);

        imageinfoService.save(imageinfoDO);

        return ServerResponse.createBySuccess(sysFile);
    }





}
